package com.health.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 过滤器-跨域问题处理(通过修改http属性)
 */
@WebFilter(urlPatterns = {"/*"})
public class CrossDomainFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        System.out.println("进入CrossDomainFilter>>>");
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        //允许可以访问的域
        response.setHeader("Access-Control-Allow-Origin", "http://localhost:63342");
        //允许的请求方式
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        //本次预检请求的有效期，单位为秒。
        response.setHeader("Access-Control-Max-Age", "3600");
        //请求头中可携带的参数
        response.setHeader("Access-Control-Allow-Headers", "*");
        //允许携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");

        filterChain.doFilter(servletRequest, servletResponse);
    }
}